package com.sgb.member.view_model;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberCompanyHeaderBean;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.ui.activity.MemberCompanyWebActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Time:2021/4/30
 * AUTHOR:F_Q
 * Description:
 */
public class MemberCompanyWebModel extends BaseViewModel {

    MemberCompanyWebActivity activity;
    MemberRepertory repertory;

    public MemberCompanyWebModel(MemberCompanyWebActivity activity, MemberRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getCompanyWebHeadInfo(MemberBean bean) {
        Map<String, Object> map = new HashMap<>();
        map.put("no", bean.getNo());
        map.put("companyNo", bean.getCompanyNo());
        DialogHelper.showProgressDialog(activity, "请求中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getCompanyWebHeadInfo(map).enqueue(new ICallBack<BaseEntity<MemberCompanyHeaderBean>>() {
            @Override
            public void onSuccess(BaseEntity<MemberCompanyHeaderBean> data) {
                if (data != null && data.getData() != null) {
                    activity.setMemberProfile(data.getData());
                } else {
                    activity.showEmptyView();
                }
            }
        });
    }

    public void getUserFocusCompanyStatus(String shopId) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getuserFocusCompanyStatus(shopId).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                activity.updateMemberFollow((Boolean) data.getData());
            }
        });
    }

    public void userFocus(String businessNo, boolean isGz) {
        Map<String, Object> map = new HashMap<>();
        map.put("businessNo", businessNo);
        map.put("type", 0); // 固定值0
        DialogHelper.showProgressDialog(activity, "请求中...").setCanceledOnTouchOutside(false);
        repertory.getApi().userFocus(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                activity.updateMemberFollow(!isGz);
            }
        });
    }

    //获取h5链接
    public void companyInfoDetail(MemberBean memberBean) {
        DialogHelper.showProgressDialog(activity, "数据加载中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getUrlSetting("companyInfoDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + memberBean.getCompanyId();// isExamine从会员审核列表进入传1
                MemberConfig.getInstance().startWebViewX5(activity, url, 101);
            }
        });
    }
}
